package com.jugheadzhou.auth.controller;

import com.jugheadzhou.auth.core.auth.LoginUser;
import com.jugheadzhou.auth.core.common.api.Result;
import com.jugheadzhou.auth.dto.LoginDto;
import com.jugheadzhou.auth.entity.User;
import com.jugheadzhou.auth.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * 用户控制层
 *
 * @author jugheadzhou
 * @date 2021-11-27
 */
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登陆
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<?> login(@Valid @RequestBody LoginDto loginDto) {
        LoginUser loginUser = userService.login(loginDto);
        return Result.success(loginUser);
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result<?> logout() {
        userService.logout();
        return Result.success("登出成功");
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public Result<?> info() {
        User user = userService.info();
        return Result.success(user);
    }

    /**
     * 更新分数
     *
     * @param username
     * @param score
     */
    @PostMapping("user/updateScore")
    public Result<?> updateScore(String username, BigDecimal score) {
        userService.updateScore(username, score);
        return Result.success();
    }


}
